package com.youbug.system.service.impl;

import com.youbug.system.entity.Job;
import com.youbug.system.mapper.JobMapper;
import com.youbug.system.schedule.AbstractJob;
import com.youbug.system.schedule.JobExecutor;
import com.youbug.system.service.IJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.youbug.system.service.IJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.youbug.system.entity.Job;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.util.Map;
import org.springframework.util.ObjectUtils;

import javax.lang.model.element.VariableElement;

/**
 * <p>
 * 定时任务 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2023-11-12
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements IJobService {

        @Autowired
        private Map<String, AbstractJob> scheduleJobMap;
        @Autowired
        private JobExecutor jobExecutor;

        @Override
        public IPage<Job> getJobPage(QueryVo<Job> queryVo) {

        Page<Job> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<Job> wrapper = new QueryWrapper<>();

        Job job =  queryVo.getPayload();

        if(!ObjectUtils.isEmpty(job)){
            if(StringUtils.hasText(job.getName())){
                wrapper.eq("name", job.getName());
            }
            if(StringUtils.hasText(job.getCode())){
                wrapper.eq("code", job.getCode());
            }
            if(StringUtils.hasText(job.getRemark())){
                wrapper.eq("remark", job.getRemark());
            }
            if(StringUtils.hasText(job.getStatus())){
                wrapper.eq("status", job.getStatus());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
            wrapper.orderByDesc("create_time");
        Page<Job> page = this.page(paramPage, wrapper);

        return page;
        }

    @Override
    public Job getJobByCode(String jobCode) {
        QueryWrapper<Job> wrapper = new QueryWrapper<>();

        wrapper.eq("code", jobCode);

        Job job = this.getOne(wrapper);

        return job;
    }

    @Override
    public boolean exec(String jobCode) {
        AbstractJob job = scheduleJobMap.get(jobCode);
        if(ObjectUtils.isEmpty(job)){
            log.error(jobCode + "任务不存在");
            return false;
        }
        if(job.getExecuting()){
            log.error(jobCode + "任务正在执行");
            return false;
        }
        jobExecutor.doExec(job, false);
        return false;
    }
}
